package com.hyh.shiyanshiguanli.controller;

import com.hyh.shiyanshiguanli.constant.MessageConstant;
import com.hyh.shiyanshiguanli.domain.dto.PublicInformationAddDTO;
import com.hyh.shiyanshiguanli.domain.dto.PublicInformationDTO;
import com.hyh.shiyanshiguanli.domain.dto.PublicInformationUpdateDTO;
import com.hyh.shiyanshiguanli.domain.entity.PublicInformation;
import com.hyh.shiyanshiguanli.domain.result.PageResult;
import com.hyh.shiyanshiguanli.domain.result.Result;
import com.hyh.shiyanshiguanli.service.PublicInformationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author hyh
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/public")
public class publicInformationController {

    @Autowired
    PublicInformationService publicInformationService;

    @GetMapping("/page")
    public Result<PageResult> page(PublicInformationDTO publicInformationDTO){
        log.info("公告信息的分页查询{}",publicInformationDTO.getMessage());
        PageResult result = publicInformationService.page(publicInformationDTO);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    public Result<PublicInformation> selectById(@PathVariable Integer id){
        log.info("根据id查询information{}",id);
        PublicInformation publicInformation = publicInformationService.selectById(id);
        return Result.success(publicInformation);
    }
    @PutMapping("/update")
    public Result updateById(@RequestBody PublicInformationUpdateDTO publicInformationUpdateDTO){
        log.info("更新information数据id为{}",publicInformationUpdateDTO.getId());
        publicInformationService.updateById(publicInformationUpdateDTO);
        return Result.success();
    }

    @PostMapping("/addPublicInformation")
    public Result addPublicInformation(@RequestBody PublicInformationAddDTO publicInformationAddDTO){
        log.info("添加information",publicInformationAddDTO);
        publicInformationService.add(publicInformationAddDTO);
        return Result.success();
    }

    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id){
        log.info("删除id为{}的公告",id);
        int delete = publicInformationService.deleteById(id);
        if (delete == MessageConstant.SUCCESS){
            return Result.success();
        }
        else{
            return Result.error("不能删除");
        }

    }
}
